package com.woyou.ui.component;

import java.util.ArrayList;
import java.util.List;

import com.citaq.ideliver.R;
import com.woyou.bean.CodeListResult;
import com.woyou.bean.ShopType;
import com.woyou.bean.rpc.ShopTypeReq;
import com.woyou.model.ShopsModel;
import com.woyou.model.ShopsModel_;
import com.woyou.service.ThreadPoolManager_;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import retrofit.RetrofitError;

/**
 * 商铺类型筛选下拉POP
 * @author longtao.li
 *
 */
public class ShopTypePop implements OnClickListener {
	
	private static Context mContext;
	private static Activity mActivity;
	private static View mAnchor;
	private static View mShade; //背景的遮罩
	private PopupWindow mPopupWindow;
	private View shopScopePopView;
	private static ShopTypePop instance;
	private ShopsModel mShopsController;
	
	private List<ShopType> scopes = new ArrayList<ShopType>();
	private ShopType scopeTemp;
	private ShopTypeListener mListener;
	
	private ShopTypePop() {
		init();
	}
	
	/**
	 * 加载店铺类型
	 * @param shopTypeReq
	 * @param imageView
	 */
	public void loadShopType(final ShopTypeReq shopTypeReq, final ImageView imageView) {
		scopes.clear();
		scopeTemp = null;
		
		ShopType all = new ShopType();
		all.num = "";
		all.sTypeId = "";
		all.sTypeName = "全部类型";
		scopes.add(0, all);
		
		ThreadPoolManager_.getInstance_(mContext).executeTask(new Runnable() {
			
			@Override
			public void run() {
				try {
					CodeListResult<ShopType> result = mShopsController.queryShopType(shopTypeReq);
					if ( result!=null&&result.getCode() == 1 ) {
						scopes.addAll(result.getList());
						mActivity.runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								imageView.setVisibility(View.VISIBLE);
								
							}
						});
					} else {
						mActivity.runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								imageView.setVisibility(View.GONE);
								
							}
						});
					}
				} catch (RetrofitError e) {
					mActivity.runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							imageView.setVisibility(View.GONE);
							
						}
					});
					switch (e.getKind()) {
					case HTTP:
						break;
					case CONVERSION:
						break;
					case NETWORK:
						break;
					case UNEXPECTED:
						break;
					}
				}
			}
		});
		
	}
	
	private void init() {
		mShopsController = ShopsModel_.getInstance_(mContext);
	}

	public static ShopTypePop getInstance(Context context, View anchor, View shade){
		mContext = context;
		mActivity = (Activity) context;
		mAnchor = anchor;
		mShade = shade;
		if( instance == null ){
			instance = new ShopTypePop();
		}
		return instance;
	}
	
	public void showPop(){
		if( scopes.size()<2 || scopes==null )
			return;
		
		if( mPopupWindow == null ){
			shopScopePopView = View.inflate(mContext, R.layout.pop_type_bar, null);
			int h = LayoutParams.WRAP_CONTENT;
			if( scopes.size() > 10 ){
				h = (int) mContext.getResources().getDimension(R.dimen.dp1100);
			}
			mPopupWindow = new PopupWindow(shopScopePopView, LayoutParams.WRAP_CONTENT, h);
			mPopupWindow.setOnDismissListener(new OnDismissListener() {
				
				@Override
				public void onDismiss() { 
					mShade.setVisibility(View.GONE);
				}
			});
//			if( scopeTemp == null ){
//				scopeTemp = scopes.get(0);
//			}
		}
		mShade.setVisibility(View.VISIBLE);
		refreshList();
		
		mPopupWindow.setBackgroundDrawable(new ColorDrawable(00000000));// 背景透明
		mPopupWindow.setFocusable(true);
		int h = (int) mContext.getResources().getDimension(R.dimen.dp0);
		mPopupWindow.showAsDropDown(mAnchor, 0, h);
	}

	private LinearLayout list;  //pop列表
	private void refreshList() {
		list = (LinearLayout) shopScopePopView.findViewById(R.id.list);
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				list.removeAllViews();
			}
		});
		
		if( scopeTemp == null )
			scopeTemp = scopes.get(0);
		
		List<ShopType> tempList = new ArrayList<ShopType>();
		tempList.addAll(scopes);
		tempList.remove(scopeTemp);
		
		for( ShopType s :tempList ){
			View child = View.inflate(mContext, R.layout.item_type_pop, null);
			TextView tv = (TextView) child.findViewById(R.id.text);
			child.setTag(s);
			child.setOnClickListener(this);
			child.setBackgroundResource(R.drawable.shop_pop_click_bg);
//			if( !TextUtils.isEmpty(s.num) ){
//				tv.setText(s.sTypeName + "  (" + s.num + ")");
//			}else{
//				tv.setText(s.sTypeName);
//			}
			tv.setText(s.sTypeName);
			
			list.addView(child);
			
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.sort_item:
			mPopupWindow.dismiss();
			mShade.setVisibility(View.GONE);
			
			ShopType s = (ShopType) v.getTag();
			TextView text = (TextView) mAnchor.findViewById(R.id.shops_filter_type_tv);
			text.setText(s.sTypeName);
			scopeTemp = s;
			refreshList();
			if( mListener != null ){
				mListener.itemClick(s);
			}
		break;

		default:
			break;
		}
		
	}
	
	/**
	 * 隐藏pop
	 */
	public void dismissPop(){
		if ( mPopupWindow!=null && mPopupWindow.isShowing() ){
			mPopupWindow.dismiss();
		}
	}
	
	public interface ShopTypeListener{
		void itemClick(ShopType sortType);
	}
	
	public void setItemClickListener(ShopTypeListener l){
		this.mListener = l;
	}


}
